/*
 * DatagramBuilder Library: serialize/deserialize data to/from datagram
 * Copyright (C) 2008 Rakov Ilya <RakovIlya@gmail.com>
 *
 * This library can be distributed under the terms of the GNU LGPLv2.
 * See the file COPYING.LIB.
 */


#ifndef DATAGRAM_LISTBUILDERIMPL_H_
#define DATAGRAM_LISTBUILDERIMPL_H_


/**
 * @file ListBuilderImpl.h
 *
 * @brief ListBuilder implementation file
 *
 * @date Dec 11, 2008
 * @author Rakov Ilya
 */



template< class ElemT, class CountT >
ListBuilder<ElemT, CountT>::
ListBuilder( Builder &builder )
  : builder_( builder ),
    count_  ( 0 )
{
  builder_ << count_;
}


template< class ElemT, class CountT >
CountT ListBuilder<ElemT, CountT>::
GetCount() const
{
  return count_.Get();
}


template< class ElemT, class CountT >
ListBuilder<ElemT, CountT>&
ListBuilder<ElemT, CountT>::operator <<( ElemT &value )
{
  builder_ << value;
  count_.Set( GetCount() + 1 );

  return *this;
}


template< class ElemT, class CountT >
ListBuilder<ElemT, CountT>&
ListBuilder<ElemT, CountT>::operator <<( const ElemT &value )
{
  builder_ << value;
  count_.Set( GetCount() + 1 );

  return *this;
}


template< class ElemT, class CountT >
ListBuilder< Tag<ElemT>, CountT >::
ListBuilder( Builder &builder )
  : listBuilder_( builder )
{
  Builder::ScopedTransaction transaction( builder );

  SerializeImpl< ElemT >::SerializeTag( builder );
}


template< class ElemT, class CountT >
CountT ListBuilder< Tag<ElemT>, CountT >::
GetCount() const
{
  return listBuilder_.GetCount();
}


template< class ElemT, class CountT >
ListBuilder< Tag<ElemT>, CountT >&
ListBuilder< Tag<ElemT>, CountT >::operator <<( ElemT &value )
{
  listBuilder_ << value;

  return *this;
}


template< class ElemT, class CountT >
ListBuilder< Tag<ElemT>, CountT >&
ListBuilder< Tag<ElemT>, CountT >::operator <<( const ElemT &value )
{
  listBuilder_ << value;

  return *this;
}


#endif /* DATAGRAM_LISTBUILDERIMPL_H_ */
